using System;
using System.Collections;
using UnityEngine;

public class GUIScoreBarLoader : MonoBehaviour
{
	public RectTransform Canvas;

	public RectTransform GUIScorePrefab;

	public RectTransform GUITeamPrefab;

	private void Start()
	{
	}

	private void OnEnable()
	{
		DrawScoreboard();
	}

	private void ClearCanvas()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	public void DrawScoreboard()
	{
		if (!(UnitZ.NetworkGameplay == null) && !(UnitZ.NetworkGameplay.playersManager == null) && !(Canvas == null) && !(GUIScorePrefab == null))
		{
			PlayersManager playersManager = UnitZ.NetworkGameplay.playersManager;
			if (playersManager.PlayerList != null)
			{
				ClearCanvas();
				int num = 0;
				foreach (string teams in playersManager.TeamsList)
				{
					if (teams != string.Empty)
					{
						GameObject gameObject = UnityEngine.Object.Instantiate(GUITeamPrefab.gameObject, Vector3.zero, Quaternion.identity);
						gameObject.transform.SetParent(Canvas.transform);
						GUITeamBar component = gameObject.GetComponent<GUITeamBar>();
						RectTransform component2 = gameObject.GetComponent<RectTransform>();
						if ((bool)component2)
						{
							component2.localScale = GUITeamPrefab.gameObject.transform.localScale;
							RectTransform rectTransform = component2;
							Vector2 sizeDelta = GUITeamPrefab.sizeDelta;
							rectTransform.anchoredPosition = new Vector2(0f, 0f - sizeDelta.y * (float)num);
						}
						if ((bool)component)
						{
							component.TeamName.text = teams;
						}
						num++;
					}
					foreach (PlayerData player in playersManager.PlayerList)
					{
						PlayerData current2 = player;
						if (teams == current2.Team)
						{
							GameObject gameObject2 = UnityEngine.Object.Instantiate(GUIScorePrefab.gameObject, Vector3.zero, Quaternion.identity);
							gameObject2.transform.SetParent(Canvas.transform);
							GUIScoreBar component3 = gameObject2.GetComponent<GUIScoreBar>();
							RectTransform component4 = gameObject2.GetComponent<RectTransform>();
							if ((bool)component4)
							{
								component4.localScale = GUIScorePrefab.gameObject.transform.localScale;
								RectTransform rectTransform2 = component4;
								Vector2 sizeDelta2 = GUIScorePrefab.sizeDelta;
								rectTransform2.anchoredPosition = new Vector2(0f, 0f - sizeDelta2.y * (float)num);
							}
							if ((bool)component3)
							{
								component3.Player = current2;
							}
							num++;
						}
					}
				}
				RectTransform canvas = Canvas;
				Vector2 sizeDelta3 = Canvas.sizeDelta;
				float x = sizeDelta3.x;
				Vector2 sizeDelta4 = GUIScorePrefab.sizeDelta;
				canvas.sizeDelta = new Vector2(x, sizeDelta4.y * (float)num);
			}
		}
	}

	private void Update()
	{
	}
}
